package com.angel.controller;

import com.angel.bean.Branches;
import com.angel.bean.Employee;
import com.angel.dto.BranchesDto;
import com.angel.dtoutlis.BranchesDtoUtil;

import com.angel.exception.StatusException;
import com.angel.service.OtherlogService;
import com.angel.utils.AdminUserUtils;
import com.angel.utils.LogUtils;
import com.github.pagehelper.PageInfo;
import com.angel.service.BranchesService;
import com.angel.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/branchesController")
public class BranchesController {

    @Resource
    private  BranchesService branchesService;

    @Resource
    private EmployeeService employeeService;
    @Autowired
    private OtherlogService otherlogService;


    /**
     * 添加分店
     *
     * @return 所有分店
     */
    @RequestMapping("/addBranches")
    @ResponseBody
    public Object addBranches(Branches branches) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        String msg ="";
        msg ="添加分店:"+branches.getBranName();
        branches.setState(1);

        int result = branchesService.insert(branches);
        if (result != 0) {
            map.put("result", "success");
        }
        LogUtils.InsertLog(employee,otherlogService,msg);
        return map;
    }


    /**
     * 逻辑删除分店
     * @param branches
     * @return
     */
    /*@RequestMapping("/delBranches")
    @ResponseBody
    public Object delBranches( Branches branches){

        System.out.println("************************************进入delBranches控制层");
        branches.setState(0);

        int result = branchesService.update(branches);
        if(result !=0){
            map.put("result", "success");
        }
        return map;
    }*/

    /**
     * 物理删除分店
     * @param branches
     * @return
     */
    @RequestMapping("/delBranches")
    @ResponseBody
    public Object delBranches( Branches branches){
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = branchesService.delete(branches);
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        String msg ="";
        msg ="删除分店:"+branches.getBranName();
        if(result !=0){
            map.put("result", "success");
        }
        LogUtils.InsertLog(employee,otherlogService,msg);
        return map;
    }
    /**
     * 查询单个分店
     * @param branches
     * @return
     */
    @RequestMapping("/findOneBranches")
    @ResponseBody
    public Object findOneBranches (Branches branches){
        Map<Object, Object> map = new HashMap<Object, Object>();
        Branches  bran = branchesService.findOneBranches(branches);
        if(bran !=null){
            map.put("bran",bran);
        }
        return map;
    }

    /***
     * 更新分店信息
     *
     * @return
     */
    @RequestMapping("/updateBranches")
    @ResponseBody
    public Object updateBranches(Branches branches) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = branchesService.update(branches);
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        String msg ="";
        msg ="更新分店信息:"+branches.getBranName();
        if (result != 0) {
            msg+="成功";
            map.put("result", "success");
        }
        LogUtils.InsertLog(employee,otherlogService,msg);
        return map;
    }



    /**
     * 查询所有员工类型(负责人：员工表,员工类型=店长)
     *
     * @return
     */
    @RequestMapping("/findEmployee")
    @ResponseBody
    public Object findEmployee( Employee employee) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        employee.setState(1);
        List<Employee> employeeList = employeeService.findEmployee(employee);

        if (employeeList != null) {
            map.put("employeeList", employeeList);
        }
        return map;
    }

    /**
     * 查询所有分店：
     * List<Branches> branchesList
     * @param branches
     * @return
     */
    @RequestMapping("/allBranches")
    @ResponseBody
    public Object Branches( Branches branches) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        branches.setState(1);

        List<Branches> branchesList = branchesService.findAll(branches);
        if (branchesList != null) {
            map.put("branchesList", branchesList);
        }
        return map;
    }


     /**
      * 查询分店信息
      *分页查询
      */
    @RequestMapping("/findBranches")
    @ExceptionHandler(value = StatusException.class) //统一处理异常
    public String findBranches(@RequestParam(value="pn",defaultValue="1")String pn,Model model) {
        //将前端传回的参数改为String类型，然后进行数据类型转换，如果出现问题，则进行异常处理，异常可以是空处理
        int pageNo =1;
        try {
            pageNo =Integer.valueOf(pn);
        }catch (Exception e){

        }

        PageHelper.startPage(pageNo, 5);

        Branches branches = new Branches();
        branches.setState(1);
        List<Branches> branchesList = branchesService.findAll(branches);
        PageInfo pageInfo = new PageInfo(branchesList);
        List<BranchesDto> dtoList = BranchesDtoUtil.getBranchesDto(branchesList,branchesService,employeeService);
        pageInfo.setList(dtoList);
        if (dtoList != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "branchesEntry";
    }

    /**
     * 模糊查询
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/searchBranches")
    @ExceptionHandler(value = StatusException.class)
    public String searchBranches(@RequestParam(value = "pn", defaultValue = "1") String pn,
                                 @RequestParam(value = "type") String type,
                                 @RequestParam(value = "content") String content,
                                 Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        content = content.trim().replaceAll(" ", "");
        PageHelper.startPage(pageNo, 5);
        Branches branches = null;
        Employee employee = null;
        List<Branches> branchesList = null;
        List<Employee> employeeList =null;
        switch (type) {
            case "branName":
                // 分店名
                branches = new Branches();
                branches.setBranName(content);
                branchesList =branchesService.searchBranches(branches);
                break;
            case "branPrinciple":
                // 负责人编号
                branches = new Branches();
                branches.setBranPrinciple(Integer.valueOf(content));
                branchesList =branchesService.searchBranches(branches);
                break;
          /*  case "emName":
                // 负责人名称
                employee = new Employee();
                employee.setEmName(content);
                employeeList = employeeService.searchEmployee(employee);
                break;*/
            default:
                break;
        }
        PageInfo pageInfo = new PageInfo(branchesList);
        List<BranchesDto> dtoList = BranchesDtoUtil.getBranchesDto(branchesList,branchesService,employeeService);
        pageInfo.setList(dtoList);
        model.addAttribute("type",type);
        model.addAttribute("content",content);
        model.addAttribute("pageInfo", pageInfo);
        return "branchesEntry2";
    }
}
